#pragma once

#include <iostream>

#include "Model.hpp"
#include "ctemplate/template.h"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";

    class View
    {

    public:
        View()
        {
        }

        //  渲染所有的Question
        void RenderingAll(const std::vector<Question> &questions, std::string *html)
        {
            //  1. 形成路径
            std::string src_html = template_path + "Allquestions.html";

            //  2. 形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("difficulty", q.difficulty);
            }

            //  3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            //  4. 开始完成渲染功能
            tpl->Expand(html, &root);
        }

        //  渲染指定Question
        void RenderingOne(const Question &q, std::string *html)
        {
            // 1. 形成路径
            std::string src_html = template_path + "Onequestion.html";

            // 2. 形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("difficulty", q.difficulty);
            root.SetValue("desc", q.desc);
            root.SetValue("header", q.header);

            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 开始完成渲染功能
            tpl->Expand(html, &root);
        }

        ~View()
        {
        }
    };
}